package com.yamaikeji.plan.controller;

import com.yamaikeji.common.annotation.Log;
import com.yamaikeji.common.core.controller.BaseController;
import com.yamaikeji.common.core.domain.AjaxResult;
import com.yamaikeji.common.enums.BusinessType;
import com.yamaikeji.plan.aop.CheckLogin;
import com.yamaikeji.system.domain.plan.PlanTask;
import com.yamaikeji.system.service.plan.IPlanTaskService;
import com.yamaikeji.system.util.IdCreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 子任务Controller
 * 
 * @author yamaikeji
 * @date 2024-12-27
 */
@RestController
@RequestMapping("/api/plan/planTask")
public class PlanTaskController extends BaseController {

    @Autowired
    private IPlanTaskService planTaskService;

    /**
     * 查询子任务列表
     */
    @CheckLogin
    @PostMapping("/getPlanTaskList")
    public AjaxResult getPlanTaskList(HttpServletRequest req, @RequestBody PlanTask planTask) {
        String userNo = (String) req.getAttribute("userNo");
        planTask.setUserNo(userNo);
        startPage();
        List<PlanTask> planTaskList = planTaskService.selectPlanTaskList(planTask);
        return AjaxResult.success(planTaskList);
    }

    /**
     * 查询子任务详情
     */
    @CheckLogin
    @PostMapping("/getPlanTask")
    public AjaxResult getPlanTask(HttpServletRequest req, @RequestBody PlanTask planTask) {
        String userNo = (String) req.getAttribute("userNo");
        planTask.setUserNo(userNo);
        planTask = planTaskService.selectPlanTask(planTask);
        return AjaxResult.success(planTask);
    }

    /**
     * 新增子任务
     */
    @CheckLogin
    @Log(title = "子任务", businessType = BusinessType.INSERT)
    @PostMapping("/addPlanTask")
    public AjaxResult addPlanTask(HttpServletRequest req, @RequestBody PlanTask planTask) {
        planTask.setPlanTaskNo(IdCreateUtil.generateNo("PT"));
        String userNo = (String) req.getAttribute("userNo");
        planTask.setUserNo(userNo);
        return AjaxResult.success(planTaskService.insertPlanTask(planTask));
    }

    /**
     * 修改子任务
     */
    @CheckLogin
    @Log(title = "子任务", businessType = BusinessType.UPDATE)
    @PostMapping("/editPlanTask")
    public AjaxResult editPlanTask(HttpServletRequest req, @RequestBody PlanTask planTask) {
        String userNo = (String) req.getAttribute("userNo");
        planTask.setUserNo(userNo);
        return AjaxResult.success(planTaskService.updatePlanTask(planTask));
    }

    /**
     * 删除子任务
     */
    @CheckLogin
    @Log(title = "子任务", businessType = BusinessType.DELETE)
    @GetMapping("/deletePlanTask")
    public AjaxResult deletePlanTask(HttpServletRequest req, @RequestBody PlanTask planTask) {
        String userNo = (String) req.getAttribute("userNo");
        planTask.setUserNo(userNo);
        return AjaxResult.success(planTaskService.deletePlanTask(planTask));
    }
}
